package com.hxzy.controller.admin.orders;

import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.resp.PageResp;
import com.hxzy.common.vo.resp.R;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.controller.admin.orders.req.OrdersReq;
import com.hxzy.controller.admin.orders.req.UpdateConsigneeReq;
import com.hxzy.controller.admin.orders.resp.OrderViewResp;
import com.hxzy.controller.admin.orders.resp.OrdersResp;
import com.hxzy.controller.admin.orders.resp.OrdersStatisticResp;
import com.hxzy.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/26 18:28
 * @Version 1.0
 */
@Api(tags = "管理后台-订单管理")
@RestController
@RequestMapping(value = "/api/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R<PageResp<OrdersResp>> search(@Valid OrdersReq req){
        AdminLoginResp current = WaimaiThreadLocal.getCurrent();
        if(current.getType()==2){
            req.setMerchantId(current.getId());
        }

        PageInfo<OrdersResp> pageInfo=this.ordersService.search(req);

        PageResp<OrdersResp> resp=new PageResp<>();
        resp.setRows(pageInfo.getList());
        resp.setTotal(pageInfo.getTotal());

        return R.okHasData(resp);
    }

    @ApiOperation(value = "订单统计数据")
    @GetMapping(value = "/statistic")
    public R<OrdersStatisticResp> ordersStatistic(){
        OrdersStatisticResp resp= this.ordersService.ordersStatistic();
        return R.okHasData(resp);
    }

    @ApiOperation(value = "订单详情")
    @GetMapping(value ="/{orderId}" )
    public R<OrderViewResp> findById(@PathVariable(value = "orderId") Long orderId){
        if(Objects.isNull(orderId)){
            return R.build(AckCode.USER_PARAM_IS_NOT_NULL);
        }

        OrderViewResp  resp=this.ordersService.findById(orderId);
        return R.okHasData(resp);
    }

    @ApiOperation(value = "更改收货人地址")
    @PostMapping(value = "/updateUserDetail")
    public R updateConsignee(@RequestBody @Valid UpdateConsigneeReq req){
        boolean result=this.ordersService.updateConsignee(req);
        return result? R.ok():R.build(AckCode.DATABASE_ERROR);
    }


}
